import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TextFieldWithDelete extends StatefulWidget {
  TextEditingController? controller;
  FocusNode? focusNode;
  List<TextInputFormatter>? inputFormatters;
  String? hintText;
  void Function(String)? onChanged;
  int? maxLines;
  bool hasDelete;

  TextFieldWithDelete(
      {Key? key,
      this.controller,
      this.focusNode,
      this.inputFormatters,
      this.hintText,
      this.onChanged,
      this.maxLines = 1,
      this.hasDelete = false})
      : super(key: key);

  @override
  _TextFieldWithDeleteState createState() => _TextFieldWithDeleteState();
}

class _TextFieldWithDeleteState extends State<TextFieldWithDelete> {
  final GlobalKey _deleteKey = GlobalKey();
  late TextEditingController _controller;
  late FocusNode _focusNode;
  @override
  void initState() {
    super.initState();
    if (widget.controller != null) {
      _controller = widget.controller!;
    } else {
      _controller = TextEditingController();
    }
    if (widget.focusNode != null) {
      _focusNode = widget.focusNode!;
    } else {
      _focusNode = FocusNode();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
              child: Stack(children: [
            TextField(
              style: TextStyle(fontSize: 14.0.sp),
              cursorHeight: 20.w,
              controller: _controller,
              focusNode: _focusNode,
              inputFormatters: widget.inputFormatters,
              maxLines: widget.maxLines,
              onChanged: (value) {
                setState(() {});
                if (widget.onChanged != null) {
                  widget.onChanged!(value);
                }
              },
              onTapOutside: (even) {
                //拿到删除按钮，判断点击的是否删除按钮，是的话不需要收起软键盘
                final RenderBox? referenceBox = _deleteKey.currentContext?.findRenderObject() as RenderBox?;
                final Offset? pos = referenceBox?.globalToLocal(even.localPosition);
                bool? within = referenceBox?.hitTest(BoxHitTestResult(), position: pos!);
                if (within == false) {
                  _focusNode.unfocus();
                }
              },
              decoration: InputDecoration(
                // contentPadding: EdgeInsets.only(bottom: 5),
                border: InputBorder.none,
                hintText: widget.hintText,
                hintStyle: TextStyle(
                  fontSize: 14.sp,
                  color: Color.fromRGBO(190, 189, 202, 1),
                ),
              ),
            )
          ])),
          //删除按钮
          Visibility(
              key: _deleteKey,
              visible: widget.hasDelete && _controller.text.isNotEmpty,
              child: GestureDetector(
                onTap: () {
                  setState(() {
                    _controller.clear();
                  });
                  if (widget.onChanged != null) {
                    widget.onChanged!('');
                  }
                },
                child: Container(
                  width: 40.w,
                  height: 40.w,
                  padding: EdgeInsets.only(left: 10.w, right: 15.w),
                  child: Image.asset(
                    'assets/images/dapp/history_input_close.png',
                  ),
                ),
              ))
        ],
      ),
    );
  }
}
